package net.lab1024.sa.admin.module.business.erp.poo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PoordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PuordDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.PuordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PoordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.dao.SaordaDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PuordEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.PuordaEntity;
import net.lab1024.sa.admin.module.business.erp.sao.domain.entity.SaordaEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PuordAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PuordQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.PuordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.PuordVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.PuordaVO;
import net.lab1024.sa.admin.module.business.erp.inv.domain.vo.TargetVo;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 请购单 Service
 *
 * @Author AKITA
 * @Date 2025-07-23 17:54:05
 * @Copyright AKITA
 */

@Service
public class PuordService {

    @Resource
    private PuordDao puordDao;
    @Resource
    private PuordaDao puordaDao;
    @Resource
    private SaordaDao saordaDao;
    @Resource
    private PoordaDao poordaDao;


    public PageResult<PuordVO> queryPage(PuordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PuordVO> list = puordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(PuordAddForm addForm) {
        PuordEntity puordEntity = SmartBeanUtil.copy(addForm, PuordEntity.class);
        puordDao.insert(puordEntity);
        List<PuordaEntity> puordaEntities = SmartBeanUtil.copyList(addForm.getLista(), PuordaEntity.class);
        for (PuordaEntity puordaEntity : puordaEntities) {
            puordaEntity.setId(puordEntity.getId());
            puordaDao.insert(puordaEntity);
        }
        return ResponseDTO.ok(puordEntity.getId());
    }


    @Transactional
    public ResponseDTO<String> update(PuordUpdateForm updateForm) {
        PuordEntity puordEntity = SmartBeanUtil.copy(updateForm, PuordEntity.class);
        List<PuordaEntity> puordaEntities = SmartBeanUtil.copyList(updateForm.getLista(), PuordaEntity.class);
        if (puordDao.updateById(puordEntity) == 0) {
            throw new RuntimeException("数据已被修改，刷新后重试！");
        }
        puordaDao.deleteById(updateForm.getId());
        for (PuordaEntity puordaEntity : puordaEntities) {
            puordaDao.insert(puordaEntity);
        }
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> batchDelete(List<Long> idList) {
        puordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long id) {





        List<PoordaEntity> entities = poordaDao.selectList(new LambdaQueryWrapper<PoordaEntity>().eq(PoordaEntity::getBaseId, id));

        if (!entities.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在采购订单，禁止删除！");
        }



        puordDao.deleteById(id);
        puordaDao.deleteById(id);
        return ResponseDTO.ok();
    }


    public ResponseDTO<PuordVO> info(Long id) {
        PuordEntity puordEntity = puordDao.selectById(id);
        List<PuordaEntity> puordaEntities = puordaDao.selectList(new LambdaQueryWrapper<PuordaEntity>().eq(PuordaEntity::getId, id));

        List<PuordaVO> puordaVOS = SmartBeanUtil.copyList(puordaEntities, PuordaVO.class);
        PuordVO copy = SmartBeanUtil.copy(puordEntity, PuordVO.class);

        ArrayList<TargetVo> targetVob = new ArrayList<>();
        for (PuordaVO puordaVO : puordaVOS) {
            if (puordaVO.getBaseId() != null ) {
                TargetVo targetVo = new TargetVo();
                targetVo.setId(puordaVO.getBaseId());
                targetVo.setLineNum(puordaVO.getBaseLineNum());
                targetVo.setItemId(puordaVO.getItemId());
                targetVo.setItemName(puordaVO.getItemName());
                targetVo.setQty(puordaVO.getQty());
                targetVob.add(targetVo);
            }
        }

        List<SaordaEntity> saordaEntities = saordaDao.selectList(new LambdaQueryWrapper<SaordaEntity>().eq(SaordaEntity::getBaseId, id));
        ArrayList<TargetVo> targetVoc = new ArrayList<>();
        for (SaordaEntity saordaEntity : saordaEntities) {
            TargetVo targetVo = new TargetVo();
            targetVo.setId(saordaEntity.getId());
            targetVo.setLineNum(saordaEntity.getLineNum());
            targetVo.setItemId(saordaEntity.getItemId());
            targetVo.setItemName(saordaEntity.getItemName());
            targetVo.setQty(saordaEntity.getQty());
            targetVoc.add(targetVo);
        }


        copy.setLista(puordaVOS);
        copy.setListb(targetVob);
        copy.setListc(targetVoc);



        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<PuordVO> infoUnexecute(Long id) {
        PuordEntity puordEntity = puordDao.selectById(id);
        PuordVO copy = SmartBeanUtil.copy(puordEntity, PuordVO.class);
        List<PuordaVO> puordaVOList = puordaDao.infoUnexecute(id);
        copy.setLista(puordaVOList);
        return ResponseDTO.ok(copy);
    }
}
